package com.hibiscusking.basic.util;

import com.hibiscusking.basic.eum.JwtRasHolder;
import com.hibiscusking.basic.jwt.JwtUtils;
import com.hibiscusking.basic.jwt.Payload;
import com.hibiscusking.basic.jwt.RsaUtils;
import com.hibiscusking.basic.jwt.UserInfo;
import com.hibiscusking.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

import java.security.PublicKey;
@Slf4j
public class LoginContextUtil {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            String header = request.getHeader("token");
            if (StringUtils.isNotBlank(header)) {
                PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
                Payload<UserInfo> infoFromToken = JwtUtils.getInfoFromToken(header, publicKey,UserInfo.class);
                if (infoFromToken.getUserInfo() != null) {
                    return infoFromToken.getUserInfo().getLogininfo();
                }
            }
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.error("payloadData is null!");
            return null;
    }
}
